#pragma once
#include<iostream>
#include<string>
#include<ctemplate/template.h>
// #include "oj_model.hpp"
#include"oj_model_mysql.hpp"
namespace ns_view{
    using namespace ns_model;
    const std::string template_path = "./template_html/";
    class View{
        public:
            View(){}
            ~View(){}
            void AllExpandHtml(const std::vector<Question> &questions, std::string& html)
            {
                //形成路径(得到模板文件的路径)
                std::string path=template_path+"all_questions.html";
                //构建数据根字典,并为每个问题创建子字典
                ctemplate::TemplateDictionary root("all_questions");
                // //for debug 
                // std::cout<<questions.size()<<std::endl;
                for(const auto& q:questions)
                {
                    ctemplate::TemplateDictionary* sub=root.AddSectionDictionary("questions_list");
                    sub->SetValue("id",std::to_string(q._id));
                    sub->SetValue("title",q._title);
                    sub->SetValue("difficulty",q._difficulty);
                }
                //加载模板文件
                ctemplate::Template* tpl=ctemplate::Template::GetTemplate(path,ctemplate::DO_NOT_STRIP); //后面的参数为保留原始样式
                //执行渲染,将数据渲染到模板文件中
                tpl->Expand(&html,&root);
            }
            void OneExpandHtml(const Question& question,std::string& html)
            {
                //形成路径
                std::string path=template_path+"one_question.html";
                //形成数据字典
                ctemplate::TemplateDictionary root("question");
                root.SetValue("id",std::to_string(question._id));
                root.SetValue("title",question._title);
                root.SetValue("difficulty",question._difficulty);
                root.SetValue("desc", question._desc);
                root.SetValue("pre_code", question._header);
                //加载模板网页
                ctemplate::Template* tpl=ctemplate::Template::GetTemplate(path,ctemplate::DO_NOT_STRIP);
                //将数据字典注入模板网页
                tpl->Expand(&html,&root);
            }
    };
}